package cn.yudao.order.controller;

import cn.yudao.order.dto.AccomplishDto;
import cn.yudao.order.service.IOrderProductAcquisitionService;
import cn.yudao.order.domain.OrderProductAcquisition;
import cn.yudao.order.query.OrderProductAcquisitionQuery;
import cn.yudao.basic.utils.AjaxResult;
import cn.yudao.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderProductAcquisition")
public class OrderProductAcquisitionController {
    @Autowired
    public IOrderProductAcquisitionService orderProductAcquisitionService;


    /**
     * 保存和修改公用的
     *
     * @param orderProductAcquisition 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrupadte(@RequestBody OrderProductAcquisition orderProductAcquisition) {
        try {
            if (orderProductAcquisition.getId() != null)
                orderProductAcquisitionService.upadte(orderProductAcquisition);
            else
                orderProductAcquisitionService.save(orderProductAcquisition);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            orderProductAcquisitionService.confirm(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public OrderProductAcquisition get(@PathVariable("id") Long id) {
        return orderProductAcquisitionService.LoadById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<OrderProductAcquisition> list() {

        return orderProductAcquisitionService.loadAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<OrderProductAcquisition> json(@RequestBody OrderProductAcquisitionQuery query) {
        return orderProductAcquisitionService.query(query);
    }


    @PostMapping("/confirm")
    public AjaxResult onSalePet(@RequestBody AccomplishDto accomplishDto) {
        try {
            orderProductAcquisitionService.complete(accomplishDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
}
